package com.biz.primus.common.support.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * PrimusLocalDateTime2EpochMillisSerializer
 *
 * 使用系统的时区, 转换Java8 LocalDateTime到milliseconds
 *
 * @author david-liu
 */
public class PrimusLocalDateTime2EpochMillisSerializer extends JsonSerializer<LocalDateTime> {

    @Override
    public void serialize(LocalDateTime localDateTime, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(String.valueOf(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
    }
}
